// pages/wode/my/profit.js

import {request} from '../../request/wx';
var Constants  = require('../../../utils/constants.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isRefresher:false,
    active: 0,
    incomeList: [],
    consumeList:[],
    dataType:1,
    refresh:false,
    refresh:false,
    pullUpOnLast:true,
    pullUpOnLast2:true,
    sorllHeight:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.loadData(true,1);
    this.loadData(true,2);

    let that = this;
    wx.getSystemInfo({
      success: function (res) {
        let clientHeight = res.windowHeight;
        let clientWidth = res.windowWidth;
        let ratio = 750 / clientWidth;
        let height = clientHeight * ratio;
        that.setData({
          sorllHeight: height+"rpx"
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    console.log('下拉刷新');
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log('上拉刷新');
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  onClick(event) {
    this.setData({
      active: event.detail.index,
      dataType: event.detail.index == 0 ? 1 : 2
    })
    /*wx.showToast({
      title: `点击标签 ${event.detail.name}`,
      icon: 'none',
    });*/
  },
  /**
   * 上拉事件
   */
  bindscrolltolower:function(e){
    if(this.data.dataType == 1){
      if(!this.data.pullUpOnLast){
        return;
      }
      this.loadData(false,this.data.dataType);
    }else {
      if (!this.data.pullUpOnLast2) {
        return;
      }
      this.loadData(false,this.data.dataType);
    }
    //wx.showToast({ title: '上拉', icon: 'none' });
  },
  bindrefresherrefresh:function(){
    this.loadData(true,this.data.dataType);
    //wx.showToast({ title: '下拉刷新', icon: 'none' });
    this.setData({
      isRefresher:true
    })
    setTimeout(() => {
      this.setData({
        isRefresher:false
      })
    }, 1000);
    
  },
  bindrefresherrestore:function(){
    this.setData({
      isRefresher:false
    })
  },
  bindrefresherabort:function(){
    this.setData({
      isRefresher:false
    })
  },
  loadData:function(refresh,type){
    if(refresh && type == 1){
      this.setData({
        refresh: refresh,
      })
    }else if(refresh && type == 2){
      this.setData({
        refresh: refresh,
      })
    }

    let openid = wx.getStorageSync('openid');
    let dataType = this.data.dataType;
    let offset = type == 1 ? this.data.incomeList.length : this.data.consumeList.length;
    if(refresh){
      offset = 0;
    }
    let data={openid,dataType,offset};
    this.setData({
      loaddingLast:true
    })
    return new Promise((reslove, reject) => {
      wx.showLoading({
        title:'加载中...'
     })
      request('balance/list','GET',data).then(res=>{
        
        reslove(res)
        this.setData({
          loaddingLast:false
        })
        wx.hideLoading() 
        
        if(res.code != 200){

          return ;
        }
        if(refresh && type == 1){
          this.setData({
            incomeList: res.data,
            pullUpOnLast:res.data.length >= Constants.PAGESIZE
          })
        }else if(refresh && type == 2){
          this.setData({
            consumeList:res.data,
            pullUpOnLast2:res.data.length >= Constants.PAGESIZE
          })
        }else{
          if(type == 1){
            this.setData({
              incomeList: this.data.incomeList.concat(res.data),
              pullUpOnLast:res.data.length >= Constants.PAGESIZE
            })
          }else{
            this.setData({
              consumeList: this.data.consumeList.concat(res.data),
              pullUpOnLast2:res.data.length >= Constants.PAGESIZE
            })
          }
        }

      })
    })

  },

})